package com.masterclient.app.businessmodule.homemodule.view.adapter.special;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eoner.baselibrary.utils.GlideUtil;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.bean.homecms.HomeCmsItemBean;
import com.masterclient.app.utils.CommonUtil;

import java.util.List;

/**
 * 作者：sean
 * 创建时间：2017/6/20 09:06
 * 功能说明：推荐品牌适配器
 */

public class RecommendBrandPagerAdapter extends PagerAdapter  {

    private List<HomeCmsItemBean> list;
    private Context context;
    private ViewPager vp;

    public RecommendBrandPagerAdapter(Context context, List<HomeCmsItemBean> list, ViewPager vp) {
        this.context = context;
        this.list = list;
        this.vp = vp;
    }

    @Override
    public int getCount() {
        int count = list == null?0:list.size();
        return count > 5 ? 5 : count;
//       return list.size()+2;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

//        position %= list.size();
        View view = LayoutInflater.from(container.getContext()).inflate(R.layout.view_recommend_brand, container, false);
        ImageView brand_iv = (ImageView) view.findViewById(R.id.brand_iv);
        TextView name = (TextView) view.findViewById(R.id.name);
        TextView sub_name = (TextView) view.findViewById(R.id.sub_name);
        HomeCmsItemBean bean = list.get(position);

        name.setText(bean.getTitle());
        sub_name.setText(bean.getSub_title());

        if(!TextUtils.isEmpty(bean.getImage())){
            GlideUtil.displayRoundImage(context,bean.getImage(),brand_iv,7,R.mipmap.defaults_1);
        }
        container.addView(view);
        view.setOnClickListener(v->{
            CommonUtil.goNewAnyWhere(context,bean.getTarget_platform(),bean.getTarget_message(), bean.getTarget(),bean.getTarget_id(),bean.getImage(),bean.getTitle(),bean.getCms_new_id());
        });

        return view;
    }

/*    @Override
    public void finishUpdate(ViewGroup container) {
        int position = vp.getCurrentItem();
        *//**
         *  第五这里获得当前的positon然后对其setCurrentItem进行变换
         *  这里设置当position=0时把position设置为图片列表的最大值
         *  是为了position=0时左滑显示最后一张，我举个例子这里ImageSize是5
         *  当position==0时设置为5，左滑就是position=4，也就是第五张图片，
         *
         *  if (position == (ImageSize+2) - 1)
         *  这个判断 (ImageSize+2)这个是给viewpager设置的页面数，这里是7
         *  当position==7-1=6时，这时viewpager就滑到头了，所以把currentItem设置为1
         *  这里设置为1还是为了能够左滑，这时左滑position=0又执行了第一个判断又设置为5，
         *  这样就实现了无限轮播的效果
         *  setCurrentItem(position,false);
         *  这里第二个参数false是消除viewpager设置item时的滑动动画，不理解的去掉它运行下就知道啥意思了
         *
         *//*
//        Log.i(TAG, "finishUpdate: "+position);
        if (position == 0) {
            position = list.size();
            vp.setCurrentItem(position,false);
        } else if (position == (list.size()+2) - 1) {
            position = 1;
            vp.setCurrentItem(position,false);
        }
    }*/

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

}
